import wepy from 'wepy'
import api from '@/api/api'
import {WXREGISTER} from '@/utils/constant'
export default class consultationList extends wepy.mixin {
  data = {
      result:"",
      identity:null,
      currentTab:0,
  }

  methods = {
    tabCheck(e) {
        this.currentTab = e.currentTarget.dataset.id; 
        let str_Status;
        switch(this.currentTab)
        {
            case "0":
                str_Status = 4;
            break;

            case "1":
                str_Status = 1;
            break;

            case "2":
                str_Status = 2;
            break;
        }
        this.queryConsultationList(this.identity,{
            ConsultationStatus:str_Status
        }).then((res) => {
            if(res.status == "success")
            {
                this.result = res.data;
                if(this.identity == 'user')
                {
                    this.initAvatar();
                }
                this.$apply();
            }
            else
            {
                wepy.showToast({
                title:res.msg, 
                icon: 'none'
                });
            }
        }).catch((err) => {
          wepy.showToast({
            title: '获取咨询列表失败，请重试！', 
            icon: 'none'
          }); 
        })
      }
  }

  async onLoad(e) { 
    this.identity = e.identity;
    this.$apply();
    let wxmember = wepy.getStorageSync(WXREGISTER)
    if(wxmember==null||wxmember==''){
        wepy.navigateTo({ url:"authorize" })
    }
    else
    {
         this.queryConsultationList(this.identity,{}).then((res) => {
            if(res.status == "success")
            {
                this.result = res.data;
                if(this.identity == 'user')
                {
                    this.initAvatar()
                }
                this.$apply();
            } 
            else
            {
                wepy.showToast({
                title:res.msg, 
                icon: 'none'
                });
            }
        }).catch((err) => {
        wepy.showModal({
            title: '提示', //提示的标题,
            content: '咨询列表获取失败，请重试！', //提示的内容,
            showCancel: false, //是否显示取消按钮,
            confirmText: '确定', //确定按钮的文字，默认为取消，最多 4 个字符,
            confirmColor: '#3CC51F', //确定按钮的文字颜色,
            success: res => {
                if(res.confirm)
                {
                    wepy.switchTab({ url: 'bang' });
                }
            }
        });
        })
    }
  }

    onShow() {

    }

  //咨询列表
  async queryConsultationList(identity,obj)
  {
      let result;
      let queryData = {};
      queryData.MemberId = await wepy.getStorageSync(WXREGISTER).id;
      queryData.PageSize = !obj.PageSize ? 10 : obj.PageSize;
      queryData.CurrentPage = !obj.CurrentPage ? 1 : obj.CurrentPage;
      queryData.ConsultationType = !obj.ConsultationType ? 1 : obj.ConsultationType;
      queryData.ConsultationStatus = !obj.ConsultationStatus ? 4 : obj.ConsultationStatus;
    if(identity == 'user')
    {
        //如果identity为用户
        result = await api.QueryMemberConsultationList({
            query:queryData
        });
    }
    else if(identity =='expert')
    {
        //如果identity为专家
        result = await api.QueryExpertConsultationList({
            query:queryData
        });
    }

    return result;
  }

  //处理专家头像
  async initAvatar()
  {
    if(this.result != '' && this.result != null)
    {
        for(let i = 0; i < this.result.length; i++)
        {
            if(typeof this.result[i].ext_expert.member_images == 'string')
            {
                this.result[i].ext_expert.member_images = await JSON.parse(this.result[i].ext_expert.member_images);
            }
        }
    }
    this.$apply();
  }
}
